/* Copyright 2012 Alejandro Alonso Mayo
This file is part of jmtools.

jmtools is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

jmtools is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with jmtools.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <jmtools/LogManager.h>

namespace JMTools
{
	namespace Logs
	{
		LogManager::LogManager()
		{
		}
	
		LogManager::~LogManager()
		{
			while(!this->systemLogs.empty())
				this->systemLogs.pop_front();
			while(!this->logs.empty())
			{
				Log * log = this->logs.front();
				this->logs.pop_front();
				delete log;
			}
		}

		Log * LogManager::getLog(std::string name)
		{
			std::list<Log *>::iterator it;
			bool encontrado = false;
			Log * log = 0;
		        for (it = this->logs.begin(); !encontrado && it != this->logs.end(); it++)
				if ((*it)->getName()==name)
				{
					log = *it;
					encontrado = true;
				}
			return log;
		}

		Log * LogManager::create(std::string name,bool system,bool cmd,LogType type, bool restricted)
		{
			Log * log = new Log(name,type,cmd,restricted);
			this->logs.push_back(log);
			if (system)
			{
				this->systemLogs.push_back(log);
				log->logMessage("Added file "+name+" to system logs.");
			}
			return log;
		}

		void LogManager::systemLogMessage(std::string message,LogType type)
		{
		        std::list<Log *>::iterator it;
		        for (it = this->systemLogs.begin(); it != this->systemLogs.end(); it++)
		                (*it)->logMessage(message,type);
		}

		void LogManager::allLogMessage(std::string message,LogType type)
		{
		        std::list<Log *>::iterator it;
		        for (it = this->logs.begin(); it != this->logs.end(); it++)
		                (*it)->logMessage(message,type);
		}
	}
}
